﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;

using IBatisNet.DataMapper;
using IBatisNet.Utilities;
using IBatisNet.DataMapper.Configuration;

namespace Leefish.Market.PRO
{
    public class Common
    {
        //注：0ED094DB3187261B是加密后的“”，因为CryptoHelper.DES_Decrypt解密非加密数据会报错

        /// <summary>
        /// 
        /// </summary>
        public static readonly string Empty = "0ED094DB3187261B";

        public const string key = "tywebweb";
        /// <summary>
        /// 框架连接串
        /// </summary>
        public static readonly string SqlConnStr = TY.Webs.ClassLib.CryptoHelper.DES_Decrypt(ConfigurationSettings.AppSettings["SqlConnStr"], key);
        //public static readonly string SqlConnStr = ConfigurationSettings.AppSettings["SqlConnStr"].ToString();
        
        /// <summary>
        /// 初始化的比对时间
        /// </summary>
        public static readonly DateTime TimeInit = DateTime.Parse("2013-5-1 0:0:0");


        /// <summary>
        /// 默认的mapper
        /// </summary>
        public static ISqlMapper DefaultMapper
        {
            get
            {
                string key = "my_mapper";
                ISqlMapper mapper = SqlMapperHelper.Instance.Get(key);
                if (mapper == null)
                {
                    try
                    {
                        System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("Leefish.Market.PRO");
                        System.IO.Stream stream = assembly.GetManifestResourceStream("Leefish.Market.PRO.sqlmap.config");
                        
                        IBatisNet.DataMapper.Configuration.DomSqlMapBuilder builder = new IBatisNet.DataMapper.Configuration.DomSqlMapBuilder();
                        System.Collections.Specialized.NameValueCollection values = new System.Collections.Specialized.NameValueCollection();
                        values.Add("connectionString", string.Format(SqlConnStr, AppDomain.CurrentDomain.BaseDirectory));
                        builder.Properties = values;
                        
                        mapper = builder.Configure(stream);

                        
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    //mapper = SqlMapperHelper.Instance.Add(key,
                    //    string.IsNullOrEmpty(SqlConnStr) ?//解密后就是为""
                    //    "" :
                    //    SqlConnStr);
                }

                return mapper;
            }
        }

       


    }
}
